<?php

namespace App\Ad\Postback;

use App\Ad\Postback\Handle\FirstupPostback;
use App\Ad\Postback\Handle\PayPostback;
use App\Ad\Postback\Handle\RegPostback;
use App\Commons\CommonConstant;
use App\Models\AdMedia;
use App\Models\AdPlan;
use App\Models\Log\LogAdPostback;
use App\Models\Orders;
use App\Models\Users;
use App\Services\ServiceBase;

class PostbackService extends ServiceBase
{
    public function addPostback($convert_type, $firstup_ad, $mod_id)
    {
        $log = LogAdPostback::where('mod_id', $mod_id)->where('convert_type', $convert_type)->first();
        if ($log) {
            return;
        }

        $plan = AdPlan::find($firstup_ad->plan_id);

        $log                   = new LogAdPostback;
        $log->create_time      = time();
        $log->convert_type     = $convert_type;
        $log->plan_id          = $firstup_ad->plan_id;
        $log->media_id         = $plan['media_id'];
        $log->media_docking_id = $plan['media_docking_id'];
        $log->game_id          = $plan['game_id'];
        $log->channel          = $plan['channel'];
        $log->firstup_ad_id    = $firstup_ad->id;
        $log->firstup_id       = $firstup_ad->firstup_id;
        $log->mod_id           = $mod_id;

        switch ($convert_type) {
            case CommonConstant::EVENT_FIRSTUP:
                $log->convert_time = $firstup_ad->create_time;
                break;
            case CommonConstant::EVENT_REG:
                $user = Users::find($mod_id);
                $log->convert_time = $user->create_time;
                $log->reg_time = $user->create_time;
                $log->user_id = $user->user_id;
                break;
            case CommonConstant::EVENT_PAY:
                $order = Orders::find($mod_id);
                $user = Users::find($order->user_id);
                $log->convert_time = $order->create_time;
                $log->reg_time = $user->create_time;
                $log->user_id = $user->user_id;
                break;
        }

        // 将媒体的广告信息赋值到 整合广告数据 里
        switch ($firstup_ad->media_id) {
            // 广点通
            case AdMedia::MEDIA_GDT:
                $log->myad_group_id    = $firstup_ad->campaign_id;
                $log->myad_ad_id       = $firstup_ad->adgroup_id;
                $log->myad_creative_id = $firstup_ad->ad_id;
                break;
            // 头条
            case AdMedia::MEDIA_TOUTIAO:
                $log->myad_group_id = $firstup_ad->project_id;
                $log->myad_ad_id    = $firstup_ad->promotion_id;
                break;
            // Bilibili
            case AdMedia::MEDIA_BILIBILI:
                $log->myad_group_id    = $firstup_ad->campaign_id;
                $log->myad_ad_id       = $firstup_ad->ad_id;
                $log->myad_creative_id = $firstup_ad->creative_id;
                break;
            // UC 快手
            case AdMedia::MEDIA_KUAISHOU:
            case AdMedia::MEDIA_UC:
                $log->myad_group_id    = $firstup_ad->adgroup_id;
                $log->myad_ad_id       = $firstup_ad->ad_id;
                $log->myad_creative_id = $firstup_ad->creative_id;
                break;
        }

        $log->save();

        return $log;
    }

    public function postback(LogAdPostback $log)
    {
        if ($log->status == LogAdPostback::STATUS_SUCC) {
            return;
        }

        $class = null;
        switch ($log->convert_type) {
            case CommonConstant::EVENT_FIRSTUP:
                $class = new FirstupPostback($log);
                break;
            case CommonConstant::EVENT_REG:
                $class = new RegPostback($log);
                break;
            case CommonConstant::EVENT_PAY:
                $class = new PayPostback($log);
                break;
        }
        if (!$class) {
            return;
        }

        return $class->handle($log);
    }

    // sdk订单通知给前端后，更新回传日志状态
    public function updateSdkPayStatus(Orders $order)
    {
        $convert_type = CommonConstant::EVENT_PAY;
        $log = LogAdPostback::where('mod_id', $order->id)->where('convert_type', $convert_type)->first();
        if (!$log) {
            return;
        }

        $class = new PayPostback($log);
        $class->postSuccessBySdk();
    }
}
